<?php

	include_once('Commons.php');
	include_once('AlbumCommons.php');
	
	$action = param('ac');
	$userParam = param('user');
	$albumParam = param('album');
	$photoParam = param('photo');
	
	$currentUser = getCurrentUser();
	
	if ( $action=='postComment' )
	{
		loginRequired( getTerm('albums', 'post_comment') );
		
		$photo = param('photoId');
		$comment = param('comment');
		
		if ( ! is_numeric($photo) )
			$photoError = 'Internal error';
		
		if ( empty($comment) )
			$commentError = getTerm('albums', 'empty_comment');
		
		if ( empty($photoError) && empty($commentError) )
		{
			$sql = "INSERT INTO comment (user,photo,comment,postdate) VALUES (${currentUser[id]},$photo,'$comment',NOW());";
			
			query($sql);
			
			redirect( ereg_replace('\?.*', '', $_SERVER['REQUEST_URI']) );
		}
	}
	elseif ( $action=='uploadphoto' )
	{
		loginRequired( getTerm('albums', 'share_photo') );
		
		$size = $_FILES['photo']['size'];
		$types = array('gif', 'jpg', 'png');
		$filesrc = $_FILES['photo']['tmp_name'];
		
		if ( $_FILES['photo']['error']==UPLOAD_ERR_NO_FILE )
			$photoError = getTerm('account', 'no_photo_file');
			
		elseif ( $_FILES['photo']['error']!=UPLOAD_ERR_OK )
			$photoError = getTerm('account', 'transfert_error');
		
		else
		{
			list($width, $height, $type, $attr) = getimagesize($filesrc);
			
			$type--;
			
			if ( $type<0 || $type>2 )
				$photoError = getTerm('account', 'bad_photo_format');
		}
		
		if ( !empty($photoError) )
			$action = 'photo';
		
		else
		{
			if ( empty($albumParam) )
			{
				$album = getAlbumByUserAndName($currentUser['id'], 'Default album');
				
				if ( empty($album) )
				{
					$albumId = createAlbum($currentUser['id'], 'Default album');
					$album = getAlbumByUserAndName($currentUser['id'], 'Default album');
				}
			}
			else
			{
				$album = getAlbumByUserAndName($currentUser['id'], $albumParam);
			}
							
			if ( empty($album) )
				$photoError = getTerm('albums', 'not_your_album');
				
			else
			{
				list($albumId) = mysql_fetch_array($rh);
				
				$photoId = processUpload($filesrc, $_FILES['photo']['name'], $types[$type], $currentUser['id'], $album['id'], $width, $height, $size);
				
				$_SESSION['message'] = getTerm('albums', 'photo_upload_successful');
				
				redirect( formatUrl("/albums/${currentUser[username]}/${album[path]}/") );
			}
		}
	}
	
	/**
	 * R�cup�ration des donn�es
	 */
	
	if ( ! empty($userParam) )
	{
		$albumUser = getUser($userParam);
		
		if ( $albumUser==-1 )
			redirect( formatUrl('/albums/') );
		
		$albums = getUserAlbums($albumUser['id']);
		
		if ( empty($albumParam) )
			$latestPhotos = getLatestUserPhotos($albumUser['id']);
	}
	else
	{
		$latestPhotos = getLatestPhotos(16, 1);
	}
	
	if ( ! empty($albumParam) )
	{
		$rh = query("	SELECT a.*
						FROM album a
						JOIN user u ON a.user=u.id
						WHERE u.username='$userParam'
						AND a.path='$albumParam'
						ORDER BY a.name;");
						
		if ( mysql_num_rows($rh)==0 )
			redirect('../');
			
		$albumInfo = mysql_fetch_assoc($rh);
		
		$currentAlbumId = $albumInfo['id'];
						
		$photoAlbum = selectQuery("	SELECT p.*
									FROM photo p
									WHERE p.album=${albumInfo[id]}
									ORDER BY p.id DESC;");
	}
	
	if ( ! empty($photoParam) )
	{
		$sql = "SELECT *
				FROM photo
				WHERE album=${albumInfo[id]}
				AND path='$photoParam';";
				
		$rh = query($sql);
						
		if ( mysql_num_rows($rh)==0 )
			redirect('../');
			
		$photoInfo = mysql_fetch_assoc($rh);
		
		$sql = "SELECT c.*,u.username, YEAR(CURDATE()) - YEAR(p.birthday) - IF( RIGHT(CURDATE(),5) < RIGHT(p.birthday,5) , 1, 0 ) AS 'age', p.gender, p.description, r.name AS 'country', p.photo, ph.type
				FROM comment c
				JOIN user u ON c.user=u.id
				JOIN profile p ON p.user=u.id AND p.current=1
				JOIN region r ON p.country=r.id
				LEFT JOIN photo ph ON p.photo=ph.id
				WHERE u.banned=0
				AND c.photo=${photoInfo[id]};";
				
		$comments = selectQuery($sql);
	}
	
	if ( array_key_exists('message', $_SESSION) )
	{
		$message = $_SESSION['message'];
		$_SESSION['message'] = '';
	}
	
	$hasWritePermission = $currentUser['id']==$albumUser['id'];
	
	if ( ! empty($photoParam) )
		require('AlbumViews/AlbumPhotoView.php');
		
	elseif ( ! empty($albumParam) )
		require('AlbumViews/GalleryView.php');
		
	elseif ( ! empty($userParam) )
		require('AlbumViews/UserGalleriesView.php');
		
	else
		require('AlbumViews/RootView.php');
		
?>